import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MarksOperations extends JFrame implements ActionListener
{
	//Singleton
	private static MarksOperations mo=null;
	
	JButton  btnSearch,btnClear,btnExit;
	JLabel lblHeading,lblStudentNo,lblEnrolledCourse,lblStudentName,lblEmpty;
	JTextField txtStudentNo;	
	JList lstEnrolledCourses;
	JScrollPane jspEnrolledCourse;
	DefaultListModel lstModelEnrolledCourses;
	
	private MarksOperations()
	{
		//Outer Panel
		JPanel screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );		
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));
		//
		
		//Inner Panel
		JPanel screenSub=new JPanel( );
		screenSub.setLayout( new GridLayout( 5,0,8,8 ));		
		//
		
		//Labels
		//Heading
		lblHeading=new JLabel(ProgramUitl.UPDATE_STUDENT_MARKS_TEXT);
		Font fontHeading=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.HEADING_FONT_STLYE,
									ProgramUitl.HEADING_FONT_SIZE);
		lblHeading.setFont(fontHeading);
		screenMain.add("North",lblHeading);		
		//
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);
									
		lblStudentNo=new JLabel(ProgramUitl.STUDNET_NO_TEXT);						
		lblStudentNo.setFont(fontLbl);
		
		lblEnrolledCourse=new JLabel(ProgramUitl.COURSE_ENROLLED_TEXT);						
		lblEnrolledCourse.setFont(fontLbl);
		
		lblStudentName=new JLabel(ProgramUitl.EMPTY);						
		lblStudentName.setFont(fontLbl);
		
		lblEmpty=new JLabel(ProgramUitl.EMPTY);						
		lblEmpty.setFont(fontLbl);	
		//

		//Text Boxes
		txtStudentNo=new JTextField();		
		//
		
		//List boxes
		lstModelEnrolledCourses = new DefaultListModel();  
		lstEnrolledCourses=new JList(lstModelEnrolledCourses);
		lstEnrolledCourses.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);	
				
		lstEnrolledCourses.addMouseListener(new MouseAdapter() 
		{
			public void mouseClicked(MouseEvent evt) 
			{
				JList lTmpEC = (JList)evt.getSource();
				if (evt.getClickCount() == 2) 
				{
					int index = lTmpEC.locationToIndex(evt.getPoint());
					if(index!=-1)
					{	
						loadMarkGui(index);
					}
				}
			}
		});		
		jspEnrolledCourse=new JScrollPane(lstEnrolledCourses);	
		
		//Buttons
		btnSearch=new JButton(ProgramUitl.SEARCH_TEXT);		
		btnClear=new JButton(ProgramUitl.CLEAR_TEXT);
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		
		
		btnSearch.addActionListener(this);
		btnClear.addActionListener(this);		
		btnExit.addActionListener(this);
		//

		//Layout Design
		screenSub.add(lblStudentNo);
		screenSub.add(txtStudentNo);
		
		screenSub.add(lblEmpty);
		screenSub.add(lblStudentName);
		
		
		screenSub.add(lblEnrolledCourse);
		screenSub.add(jspEnrolledCourse);						
		
		screenSub.add(btnSearch);		
		screenSub.add(btnClear);		
		screenSub.add(btnExit);		
		//
		
		//Inner panel added to Outer Panel 
		screenMain.add("Center",screenSub);
		//
		
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});

		
		setSize(ProgramUitl.SMALL_SCREEN_WIDTH,ProgramUitl.MEDIUM_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(true);			
		setVisible(true);
		
		validate();
		//		
	}
	
	public static MarksOperations getInstance()
	{
		if(mo==null)
		{
			mo=new MarksOperations();
		}
		
		return mo;
	}
	
	//	

	//Methods
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.UPDATE_STUDENT_MARKS_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				MainMenu.getInstance().setEnabled(true);
				MainMenu.getInstance().setVisible(true);
				mo=null;
				this.dispose();
				
		} 
	}
	
	void clearText()
	{
		txtStudentNo.setText(null);	
		lblEmpty.setText(null);	
		lblStudentName.setText(null);
	
		lstModelEnrolledCourses.removeAllElements();	
		
		txtStudentNo.requestFocus();		
	}
	
	public boolean isValidInputs()
	{
		String msg="";
		//Student No
		if(ProgramUitl.IsEmptyString(txtStudentNo.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentNo.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.isPositiveWithNumeric(txtStudentNo.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentNo.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		if(ProgramUitl.IsEmptyString(msg)!=true)
		{
			JOptionPane.showMessageDialog(null,msg,ProgramUitl.UPDATE_STUDENT_MARKS_TEXT,JOptionPane.ERROR_MESSAGE);
		}
		

		return ProgramUitl.IsEmptyString(msg);		
	}
	
	void seachStudent()
	{		
		if(isValidInputs())
		{
			lstModelEnrolledCourses.removeAllElements();		
			Student s=XmlOperations.getInstance().searchStudentByNo(txtStudentNo.getText().trim());	
			if(s!=null)
			{	
				lblEmpty.setText(ProgramUitl.STUDNET_NAME_TEXT);	
				lblStudentName.setText(s.getName());
				populateEnrolledCourse(txtStudentNo.getText().trim());
			}
			else
			{				
				JOptionPane.showMessageDialog(null,ProgramUitl.OBJECT_NOT_FOUND,ProgramUitl.UPDATE_STUDENT_MARKS_TEXT,JOptionPane.ERROR_MESSAGE);
				txtStudentNo.requestFocus();
			}
		}
	}
	
	void populateEnrolledCourse(String studenNo)
	{
		java.util.List<Enroll> l=XmlOperations.getInstance().getStudentEnrolInfo(studenNo);		
		if(l!=null)
		{			
			for (int i = 0; i < l.size(); i++) 
			{	
				Enroll e=((Enroll)l.get(i));
				Course c=XmlOperations.getInstance().searchCourseByNo(Integer.toString(e.getCode()));
				lstModelEnrolledCourses.addElement(ProgramUitl.buildCourseListItem(c));
				lstEnrolledCourses=new JList(lstModelEnrolledCourses);				
			}
		}
	}	
	
	public void toggleAllButtonsStatus(boolean enbaled)
	{
		btnSearch.setEnabled (enbaled);
		btnClear.setEnabled (enbaled);
		btnExit.setEnabled (enbaled);
		lstEnrolledCourses.setEnabled (enbaled);
	}
	
	void loadMarkGui(int index)
	{		
		Enroll e=XmlOperations.getInstance().getStudentEnrolInfoByStNoCoId(txtStudentNo.getText().trim(),
		Integer.toString(ProgramUitl.getCourseNoFromListItem((String)lstModelEnrolledCourses.getElementAt(index))));
		MarkGui.getInstance(e);
	}
	//

	//Events
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}	
		else if(ae.getSource().equals(btnSearch))
		{		
			seachStudent();
		}
		else if(ae.getSource().equals(btnClear))
		{			
			clearText();			
		}		
	}	
	//	
}